package com.zggk.iroad.handle.handlelist;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zggk.iroad.R;
import com.zggk.iroad.adapter.listview.CommonAdapter;
import com.zggk.iroad.adapter.listview.ViewHolder;
import com.zggk.iroad.adapter.recycleview.LGRecycleViewAdapter;
import com.zggk.iroad.adapter.recycleview.LGViewHolder;
import com.zggk.iroad.db.CzfaInfo;
import com.zggk.iroad.db.EntityManager;
import com.zggk.iroad.db.HandleInfo;
import com.zggk.iroad.diease.dieasedb.DieaseDbActivity;
import com.zggk.iroad.diease.dieasemx.DieaseMxActivity;
import com.zggk.iroad.greendao.CzfaInfoDao;
import com.zggk.iroad.greendao.HandleInfoDao;
import com.zggk.iroad.handle.handleadd.HandleAddActivity;
import com.zggk.iroad.handle.handledb.HandleDbActivity;
import com.zggk.iroad.handle.handlexb.HandleXbActivity;
import com.zggk.iroad.mvp.MVPBaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.blankj.utilcode.util.SizeUtils.dp2px;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class HandleListActivity extends MVPBaseActivity<HandleListContract.View, HandleListPresenter> implements HandleListContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.lay_xm)
    LinearLayout layXm;
    @Bind(R.id.log_rv)
    SwipeMenuListView waihandleRv;
    @Bind(R.id.sm_lay)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.add_lay)
    TextView addLay;

    private List<HandleInfo> Datas = new ArrayList();
    private HandleInfoDao handleInfoDao;
    private CzfaInfoDao czfaInfoDao;
    private CommonAdapter<HandleInfo> adapter;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_handle_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initAdapter();
        initView();
        initData();
    }

    private void initAdapter() {
        waihandleRv.setMenuCreator(creator);
        adapter = new CommonAdapter<HandleInfo>(HandleListActivity.this,
                R.layout.item_diease, Datas) {
            @Override
            protected void convert(final ViewHolder holder, HandleInfo zCDBean, final int position) {
                holder.setImageUrl(R.id.dieast_img, zCDBean.getPIC());
                holder.setText(R.id.disease_lxbm, zCDBean.getLXBM());
                holder.setText(R.id.disease_bhmc, zCDBean.getBHLX());
                holder.setText(R.id.disease_sj, zCDBean.getYQWCSJ());
                if (zCDBean.getISDB().equals("1")) {
                    holder.setText(R.id.disease_zt, "未上传");
                    holder.setTextColor(R.id.disease_zt, Color.parseColor("#cf1f1f"));
                } else if (zCDBean.getISDB().equals("0")) {
                    holder.setText(R.id.disease_zt, "待施工");
                    holder.setTextColor(R.id.disease_zt, Color.parseColor("#2ec849"));
                }
            }
        };
        waihandleRv.setAdapter(adapter);
        waihandleRv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (Datas.get(position).getISDB().equals("0")) {//不是本地
                    Intent intent = new Intent(HandleListActivity.this, HandleAddActivity.class);
                    intent.putExtra("RZID", Datas.get(position).getId());
                    startActivityForResult(intent, 1);
                } else {//是本地
                    Intent intent = new Intent(HandleListActivity.this, HandleDbActivity.class);
                    intent.putExtra("RZID", Datas.get(position).getId());
                    startActivityForResult(intent, 1);
                }
            }
        });
    }

    private void initData() {
        refreshLayout.autoRefresh();
    }

    private void initView() {
        handleInfoDao = EntityManager.getInstance().getHandleInfoDao();
        czfaInfoDao = EntityManager.getInstance().getCzfaInfoDao();
        titleText.setText("施工维修");
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (NetworkUtils.isAvailableByPing()) {//有网
                    mPresenter.testinfo();
                } else {
                    Datas.clear();
                    Datas.addAll(handleInfoDao.loadAll());
                    adapter.notifyDataSetChanged();
                    if (refreshLayout.isRefreshing()) {
                        refreshLayout.finishRefresh();
                    }
                }
            }
        });
        waihandleRv.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                            Intent intent = new Intent(HandleListActivity.this, HandleXbActivity.class);
                            intent.putExtra("RZID", Datas.get(position).getId());
                            startActivityForResult(intent, 1);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void getDatas(List<HandleBean> HandleBeans) {
        if (HandleBeans != null && HandleBeans.size() > 0) {
            handleInfoDao.deleteAll();
            czfaInfoDao.deleteAll();
            for (int i = 0; i < HandleBeans.size(); i++) {
                HandleInfo handleInfo = new HandleInfo();
                handleInfo.setBHGUID(HandleBeans.get(i).getBHGUID());
                handleInfo.setDCR(HandleBeans.get(i).getDCR());
                handleInfo.setYQWCSJ(HandleBeans.get(i).getYQWCSJ());
                handleInfo.setLXBM(HandleBeans.get(i).getLXBM());
                handleInfo.setQDZH(HandleBeans.get(i).getQDZH());
                handleInfo.setCZFAMC(HandleBeans.get(i).getCZFAMC());
                handleInfo.setSGMX(HandleBeans.get(i).getSGMX());
                handleInfo.setTJQDZH(HandleBeans.get(i).getTJQDZH());
                handleInfo.setYGSL(HandleBeans.get(i).getYGSL());
                handleInfo.setBH(HandleBeans.get(i).getBH());
                handleInfo.setWZFX(HandleBeans.get(i).getWZFX());
                handleInfo.setBHLX(HandleBeans.get(i).getBHLX());
                handleInfo.setJLDW(HandleBeans.get(i).getJLDW());
                handleInfo.setZZXH(HandleBeans.get(i).getZZXH());
                handleInfo.setBZ(HandleBeans.get(i).getBZ());
                handleInfo.setISDB("0");
                if (HandleBeans.get(i).getPIC() != null && HandleBeans.get(i).getPIC().size() > 0) {
                    handleInfo.setPIC(HandleBeans.get(i).getPIC().get(0).getFILEPATH());
                }
                handleInfoDao.insert(handleInfo);
                for (int j = 0; j < HandleBeans.get(i).getCZFAMX().size(); j++) {
                    CzfaInfo czfaInfo = new CzfaInfo();
                    czfaInfo.setGCXM(HandleBeans.get(i).getCZFAMX().get(j).getGCXM());
                    czfaInfo.setGCXMID(HandleBeans.get(i).getCZFAMX().get(j).getGCXMID());
                    czfaInfo.setJSGS(HandleBeans.get(i).getCZFAMX().get(j).getJSGS());
                    czfaInfo.setSL(HandleBeans.get(i).getCZFAMX().get(j).getSL());
                    czfaInfo.setDW(HandleBeans.get(i).getCZFAMX().get(j).getDW());
                    czfaInfo.setParentId(handleInfo.getId());
                    czfaInfoDao.insert(czfaInfo);
            }
            }
            Datas.clear();
            Datas.addAll(handleInfoDao.loadAll());
        }
        adapter.notifyDataSetChanged();
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
    }

    @Override
    public void deleteByIds() {
        refreshLayout.autoRefresh();
    }

    SwipeMenuCreator creator = new SwipeMenuCreator() {
        @Override
        public void create(SwipeMenu menu) {
            SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
            deleteItem.setBackground(R.color.theme_color);
            deleteItem.setWidth(dp2px(90));
            deleteItem.setTitle("续报");
            deleteItem.setTitleSize(18);
            deleteItem.setTitleColor(Color.WHITE);
            menu.addMenuItem(deleteItem);
        }
    };
}
